package com.dunjx.oauth.controller;

import com.alibaba.fastjson.JSON;
import com.dunjx.oauth.service.UserLoginService;
import com.dunjx.oauth.uitl.AuthToken;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

/**
 * @author ha
 */
@RestController
@RequestMapping("/user")
public class UserLoginController {

    /**
     * 客户端id
     */
    @Value("${clientId}")
    private String clientId;
    /**
     * 客户端秘钥
     */
    @Value("${clientSecret}")
    private String clientSecret;


    @Autowired
    private UserLoginService userLoginService;


    private static final String GRAND_TYPE = "password";//授权模式 密码模式


    /**
     * 登录方法
     */
    @RequestMapping("/login")
    public Result login(String username, String password) {

        AuthToken authToken = null;
        try {
            authToken = userLoginService.login(username, password, clientId, clientSecret, GRAND_TYPE);
        } catch (Exception e) {
            return new Result(true, StatusCode.ERROR, "用户名或密码错误!", authToken);
        }
        return new Result(true, StatusCode.OK, "登录成功!", authToken);

    }
}
